# __coding:utf-8__
"""采集简书爬虫脚本"""
from furong_link_caiji.common.function import *;
import requests;
import json;
from datetime import datetime;

# 创建缓存目录
mkdir('../cache');
# 请求参数
params = {
    'page': 1,
    'count': 50,
    'order_by': 'added_at',
};
# 伪造请求头信息
headers = {
    'Accept': 'application/json',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'zh-CN,zh;q=0.8,en;q=0.6',
    'Connection': 'keep-alive',
    'Cookie': 'read_mode=day; default_font=font2; sajssdk_2015_cross_new_user=1; signin_redirect=https%3A%2F%2Fwww.jianshu.com%2Fp%2F14c98ab99d19; locale=zh-CN; _m7e_session=3ab0b910e0761f27943aa07fac6859df; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%2216219553a34a12-0ff4a95d54def1-574e6e46-250125-16219553a3511b%22%2C%22%24device_id%22%3A%2216219553a34a12-0ff4a95d54def1-574e6e46-250125-16219553a3511b%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E4%BB%98%E8%B4%B9%E5%B9%BF%E5%91%8A%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24latest_referrer_host%22%3A%22%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_utm_source%22%3A%22mobile%22%2C%22%24latest_utm_medium%22%3A%22collections%22%7D%7D; Hm_lvt_0c0e9d9b1e7d617b3e6842e85b9fb068=1520843439; Hm_lpvt_0c0e9d9b1e7d617b3e6842e85b9fb068=1520843766',
    'Host': 'www.jianshu.com',
    'If-None-Match': 'W/"633111a7edd5cfa5c05a5c8fb69a0579"',
    'Referer': 'https://www.jianshu.com/c/NEt52a?utm_source=mobile&utm_medium=collections',
    'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1',
}
cache_key = 'jianshu_post_list_' + datetime.strftime(datetime.today(), '%Y-%m-%d');
result = get_file_cache(cache_key);
if (result == None):
    result = requests.get('https://www.jianshu.com/mobile/collections/16/public_notes', params=params, headers=headers);
    data = json.loads(result.text);
    set_file_cache(cache_key, data);

for item in data:
    print(item['object'])
